<?php

namespace app\api\controller;

use think\Controller;
use think\Request;
use app\data\Data;
use think\Db;
use app\api\model\News as news_list;

class Material extends Index
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $page = (isset($_GET['page']) && !empty($_GET['page'])) ? $_GET['page'] : 1;
        $username = (isset($_GET['username']) && !empty($_GET['username']) ? $_GET['username'] : null);
        $logmin = (isset($_GET['logmin']) && $_GET['logmin']) ? strtotime($_GET['logmin']) : 0;
        $logmax = (isset($_GET['logmax']) && $_GET['logmax']) ? (strtotime($_GET['logmax']) + 24 * 60 * 60 - 1) : time();


        $where['static'] = (isset($_GET['static']) && !empty($_GET['static'])) ? $_GET['static'] : array('neq', 0);
        $where['time'] = array(array('egt', $logmin), array('elt', $logmax), 'and');
        $where['name'] = array('like', "%$username%");

        $zgh_material = new Data('material');
        $res['data'] = $zgh_material->selectPage($page, $where);
        $res['count'] = $zgh_material->count($where);
        if (isset($res) || !empty($res)) {
            $this->_jsonp('200', '获取素材库信息成功', $res);
        } else {
            $this->_jsonp('1000', '获取素材库信息失败', null);
        }
    }


    /**
     * 未发布改发布
     */
    public function news_up()
    {
		$true = auth();
		if ($true == 2) {
			$where['id'] = trim(input('get.id'));
			$data['static'] = 1;
			$zgh_material = new Data('material');
			$res = $zgh_material->_update($data, $where);
			if (isset($res) || empty($res)) {
				action_log('素材图未发布改发布', 'log_admin', 1);
				$this->_jsonp('200', '修改成功', $res);
			} else {
				action_log('素材图未发布改发布', 'log_admin');
				$this->_jsonp('1000', '修改失败', null);
			}
		} else if($true == 1) {
            $this->_jsonp('1000', '您没有权限', null);
        }else{
			$this->_jsonp('1000','您未登录',null);
		}
    }

    /**
     * 已发布下架
     */
    public function news_down()
    {
		$true = auth();
		if ($true == 2) {
			$where['id'] = trim(input('get.id'));
			$data['static'] = 2;
			$zgh_material = new Data('material');
			$res = $zgh_material->_update($data, $where);
			if (isset($res) || empty($res)) {
				action_log('素材图发布下架', 'log_admin', 1);
				$this->_jsonp('200', '修改成功', $res);
			} else {
				action_log('素材图发布下架', 'log_admin');
				$this->_jsonp('1000', '修改失败', null);
			}
		} else if($true == 1) {
            $this->_jsonp('1000', '您没有权限', null);
        }else{
			$this->_jsonp('1000','您未登录',null);
		}
    }

    /**
     * 删除一条
     */
    public function delete()
    {
		$true = auth();
		if ($true == 2) {
			$id = trim(input('get.id'));
			$zgh_material = new Data('material');
			$res = $zgh_material->deleteDaPtaByPrimaryKey($id);
			if ($res) {
				action_log('素材图删除', 'log_admin', 1);
				$this->_jsonp('200', '成功', $res);
			} else {
				action_log('素材图删除', 'log_admin');
				$this->_jsonp('1000', '失败', null);
			}
		} else if($true == 1) {
            $this->_jsonp('1000', '您没有权限', null);
        }else{
			$this->_jsonp('1000','您未登录',null);
		}
    }

    /**
     * 删除多条
     */
    public function del_all()
    {
		$true = auth();
		if ($true == 2) {
			$id = trim(input('get.id'));
			$where['id'] = explode(",", $id);
			for ($i = 0; $i < count($where['id']); $i++) {
				$res = db('material')->where('id', $where['id'][$i])->delete();
			}
			if ($res) {
				action_log('素材图删除', 'log_admin', 1);
				$this->_jsonp('200', '成功', $res);
			} else {
				action_log('素材图删除', 'log_admin');
				$this->_jsonp('1000', '失败', null);
			}
		} else if($true == 1) {
            $this->_jsonp('1000', '您没有权限', null);
        }else{
			$this->_jsonp('1000','您未登录',null);
		}
    }

    /**
     * 新增
     */

    public function material_add()
    {
        $files = request()->file('img');
        $news_list = new news_list();
        if (!empty($files)) {
            $img = $news_list->upload($files, 'Images/tradingMarket');
        }
        $arr['image'] = $img;
        $data['name'] = input('post.name');
        $data['time'] = time();
        $result = array_merge($data, $arr);
        //echo json_encode($data);die;
        $res = Db::name('material')->insert($result);
        if (isset($res) && !empty($res)) {
            action_log('素材图新增', 'log_admin', 1);
            $this->json('200', '添加成功', $res);
        } else {
            action_log('素材图新增', 'log_admin');
            $this->json('1000', '添加失败', null);
        }
    }

    /**
     * 编辑显示
     */
    public function material_edit_list()
    {
        $where['id'] = trim(input('get.id'));
        $zgh_material = new Data('material');
        $res = $zgh_material->_find($where);
        if ($res) {
            $this->_jsonp('200', '成功', $res);
        } else {
            $this->_jsonp('1000', '失败', null);
        }
    }

    /**
     * 编辑更新
     */
    public function material_edit()
    {
        $news_list = new news_list();
        $arr = array();
        if (!empty($_FILES)) {
            $files = request()->file('img');
            $img = $news_list->upload($files, 'Images/tradingMarket');
            $arr['image'] = $img;
        }
        $id = input('post.id');
        $data['name'] = input('post.name');
        $result = array_merge($data, $arr);
        //echo json_encode($data);die;
        $res = Db::name('material')->where('id', $id)->update($result);
        if (isset($res) && !empty($res)) {
            action_log('素材图编辑', 'log_admin', 1);
            $this->json('200', '修改成功', $res);
        } else {
            action_log('素材图编辑', 'log_admin');
            $this->json('1000', '修改失败', null);
        }
    }

}
